/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "imgops/CvWhiteBalanceOp.h"

namespace NAMESPACE{

// ****** Functor implementation ***********************************************
bool CvWhiteBalanceFunc::operator()(const cv::Mat& src, 
                                    cv::Mat& dst,
                                    WB_METHOD method){
    white_balance(src, dst, method);
    return true;
}
// -----------------------------------------------------------------------------

// ****** Constants assignment *************************************************
const size_t CvWhiteBalanceOp::MIN_INPUT_NUM = 1;
const size_t CvWhiteBalanceOp::MIN_CRITERIA_NUM = 1;
const size_t CvWhiteBalanceOp::MIN_OUTPUT_NUM = 1;
// -----------------------------------------------------------------------------

// ****** Operation implementation *********************************************
CvWhiteBalanceOp::~CvWhiteBalanceOp(){}
CvWhiteBalanceOp::CvWhiteBalanceOp() : AbstractOperation<CvWhiteBalanceFunc>(){

    min_input_num = MIN_INPUT_NUM;
    min_criteria_num = MIN_CRITERIA_NUM;
    min_output_num = MIN_OUTPUT_NUM;

    k1.set_key(INPUT_NS,  "img"); k1.setType(TypeParseTraits<cv::Mat*>::name());
    k2.set_key(OUTPUT_NS, "img"); k2.setType(TypeParseTraits<cv::Mat*>::name());

    k3.set_key(CRITERIA_NS, "constant.1"); k3.setType(TypeParseTraits<WB_METHOD*>::name());

    input_keys->push_back(k1);
    output_keys->push_back(k2);

    criteria_keys->push_back(k3);
}
CvWhiteBalanceOp::CvWhiteBalanceOp(const CvWhiteBalanceOp& gbo) : AbstractOperation<CvWhiteBalanceFunc>(gbo){}
CvWhiteBalanceOp& CvWhiteBalanceOp::operator=(const CvWhiteBalanceOp& gbo){
    if(this == &gbo) return *this;
    AbstractOperation<CvWhiteBalanceFunc>::operator=(gbo);
    return *this;
}

bool CvWhiteBalanceOp::doOperation(const std::list<input_t>& inputs,
                              const std::list<criteria_t>& operationCriteria,
                              std::list<output_t>& outputs) throw(bad_io_mapping) {

    AbstractOperation<CvWhiteBalanceFunc>::prepareIO(inputs, operationCriteria, outputs);

    cv::Mat* in_img  = boost::any_cast<cv::Mat*>( ((*input_mapping) [k1])->getElement() );
    cv::Mat* out_img = boost::any_cast<cv::Mat*>( ((*output_mapping)[k2])->getElement() );

    WB_METHOD* method  = boost::any_cast<WB_METHOD*>( ((*criteria_mapping)[k3])->getElement() );

    (*operation)(*in_img, *out_img, *method);

    return true;
}

bool CvWhiteBalanceOp::operator==(const AbstractOperation<operation_t>& op) const{
    return AbstractOperation<CvWhiteBalanceFunc>::operator==(op);
}

bool CvWhiteBalanceOp::operator!=(const AbstractOperation<operation_t>& op) const{
    return !operator==(op);
}
// -----------------------------------------------------------------------------

}
